package com.hup.minicontroller.common.util;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * @author hugan
 * @date 2021/5/11
 */
public abstract class NetUtil {

    /**
     * longIP地址转字符串
     *
     * @param value {@link WifiManager#getConnectionInfo()}, {@link WifiInfo#getIpAddress()}
     */
    public static String getIp(long value) {
        return (int) (value & 0xff) + "."
                + (int) ((value >> 8) & 0xff) + '.'
                + (int) ((value >> 16) & 0xff) + '.'
                + (int) ((value >> 24) & 0xff);
    }

    /**
     * 获取wifi的网关地址
     *
     * @return defValue-wifi未连接
     */
    public static String getGatewayIp(Context context, String defValue) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            return defValue;
        }
        DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
        return getIp(dhcpInfo.gateway);
    }

}
